package com.yng.simbot01.Handle;

import com.yng.simbot01.service.ChatEventService;
import com.yng.simbot01.service.KeyWordService;
import com.yng.simbot01.service.RobotCommandService;
import lombok.extern.slf4j.Slf4j;
import love.forte.simbot.event.ChatGroupMessageEvent;
import love.forte.simbot.event.ContactMessageEvent;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.quantcat.common.annotations.Filter;
import love.forte.simbot.quantcat.common.annotations.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;

@Component
@Slf4j
public class MessageEventHandles {

    @Autowired
    RobotCommandService robotCommandService;

    @Autowired
    ChatEventService chatEventService;

    @Autowired
    KeyWordService keyWordService;

    //群聊
    @Listener(priority = 5)
    @Filter(priority = 5)
    public CompletableFuture<?> listenGroupUser(ChatGroupMessageEvent event) {
        CompletableFuture<?> result = null;

        //指令
        try {
            result = robotCommandService.doService(event);
            if(result != null) return result;
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        //关键字过滤
        result = keyWordService.KeyWordFilter(event);
        if(result != null) return result;

        //AI
        result = chatEventService.chatWithKeyWordAndAI(event);

        //统一返回
        return result;
    }

    //私聊
    @Listener(priority = 5)
    @Filter(priority = 5)
    public CompletableFuture<?> listenSingleUser(ContactMessageEvent event) {
        CompletableFuture<?> result = null;
        //指令
        try {
            result = robotCommandService.doService(event);
            if(result != null) return result;
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        //关键字过滤
        result = keyWordService.KeyWordFilter(event);
        if(result != null) return result;

        return result;
    }


//    //priority越小越容易触发，这和线程优先级的反的
//    @Listener(priority = 1)
//    @Filter(value = "教我Java", priority = 1,matchType = MatchType.TEXT_CONTAINS, mode = FilterMode.INTERCEPTOR)
//    public CompletableFuture<?> onEventKeyWord(ChatGroupMessageEvent event) {
//        MessageContent messageContent = event.getMessageContent();
//        String plainText = messageContent.getPlainText();
//
//        return event.replyAsync("教锤子，自己学");
//    }
}